package de.jeeyeon.util;

import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class EmoKeyboard 
{
	private KeyboardView keyboardView;
	private Activity hostActivity;
	private EditText textarea;
	
	
	public EmoKeyboard(Activity host, int viewid, int layoutid, EditText textfield)
	{
		hostActivity = host;
		keyboardView = (KeyboardView) hostActivity.findViewById(viewid);
		keyboardView.setKeyboard(new Keyboard(hostActivity, layoutid));
		keyboardView.setPreviewEnabled(false);
		textarea = textfield;
		
		
		hostActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		
		keyboardView.setOnKeyboardActionListener(keyboardListener);
		
	}
	
	public void hideCustomKeyboard() 
	{
	    keyboardView.setVisibility(View.GONE);
	    keyboardView.setEnabled(false);
	}
	
	public void showCustomKeyboard( View v )
	{
	    keyboardView.setVisibility(View.VISIBLE);
	    keyboardView.setEnabled(true);
	    if( v!=null )
	    {
	    	((InputMethodManager)hostActivity.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
	    }
	}
	
	public boolean isCustomKeyboardVisible()
	{
	    return keyboardView.getVisibility() == View.VISIBLE;
	}
	
	
	private OnKeyboardActionListener keyboardListener = new OnKeyboardActionListener() 
	{
		
		@Override
		public void swipeUp() {	}
		
		@Override
		public void swipeRight() {	}
		
		@Override
		public void swipeLeft() {	}
		
		@Override
		public void swipeDown() {	}
		
		@Override
		public void onText(CharSequence text) {	  }
		
		@Override
		public void onRelease(int primaryCode) {	}
		
		@Override
		public void onPress(int primaryCode) {	}
		
		@Override
		public void onKey(int primaryCode, int[] keyCodes) 
		{
		    Editable editable = textarea.getText();
		    int start = textarea.getSelectionStart();
		    
		    if(primaryCode == 55001)
		    {
		    	editable.insert(start, ":-)");
		    }
		    
		    if(primaryCode == 55002)
		    {
		    	editable.insert(start, ";-)");
		    }
		    
		    if(primaryCode == 55003)
		    {
		    	editable.insert(start, ";-P");
		    }
		    
		    if(primaryCode == 55004)
		    {
		    	editable.insert(start, ":-J");
		    }
			
		}
	};
	
}
